package com.device.operating.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.device.operating.dao.GroupRepository;
import com.device.operating.dao.UgRepository;
import com.device.operating.entity.Usergroup;
import com.device.operating.service.UgService;

@Service("UgService")
@Transactional(propagation=Propagation.REQUIRED)
public class UgServiceImpl implements UgService{
	@PersistenceContext
	private EntityManager em;
	
	@Autowired
	private GroupRepository GroupRepository;
	@Autowired
	private UgRepository UgRepository;
	@Override
	public void addug(Usergroup ug) {
		// TODO Auto-generated method stub
		UgRepository.save(ug);
	}
	@Override
	public List<Usergroup> querybygpId(String gpId) {
		// TODO Auto-generated method stub
		return UgRepository.findByGpId(gpId);
	}
	@Override
	public List<Usergroup> querybyuserId(String userId) {
		// TODO Auto-generated method stub
		return UgRepository.findByUserId(userId);
	}
	@Override
	public void deleteug(String ugId) {
		// TODO Auto-generated method stub
		UgRepository.delete(ugId);
	}
	@Override
	public Map querybygpIdall(String gpId,int fr, int mr) {
		// TODO Auto-generated method stub
		Map map=new HashMap<>();
		String sql1="select usergroup from Usergroup usergroup where gpId='"+gpId+"'";
		String sql2="select count(*) from Usergroup usergroup where gpId='"+gpId+"'";
		Query query1=em.createQuery(sql1);
		Query query2=em.createQuery(sql2);
		int count =new Integer(query2.getSingleResult().toString()).intValue();
	    query1.setFirstResult(fr*mr);
	    query1.setMaxResults(mr);
	    List<Usergroup> lug=query1.getResultList();
	    map.put("rows", lug);
	    map.put("total", count);
		return map;
	}
	@Override
	public Usergroup querybyuserIdandgpId(String userId, String gpId) {
		// TODO Auto-generated method stub
		return UgRepository.findByUserIdAndGpId(userId, gpId);
	}
	@Override
	public List<Usergroup> querybyrootdpId(String rootdpId) {
		// TODO Auto-generated method stub
		return UgRepository.findByRootdpId(rootdpId);
	}
	@Override
	public List<Usergroup> querybydpId(String dpId) {
		// TODO Auto-generated method stub
		return UgRepository.findByDpId(dpId);
	}


	
}
